'''

Case Type： pid文件
Case Name： 数据库运行时丢失pid文件
Create At:  2020/06/30
Owner    :  n@ningyali
Descption:  1.删除pid文件 2.查看pid文件（不会自动生成） 3.执行sql并行脚本，查看对数据库的影响 4.查看pid文件（不会自动生成） 5.还原

'''

import os
import sys
import unittest
import time
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *


class PidLostWithRun(unittest.TestCase):
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case076.py start------------')
        self.pid_file_path = os.path.join(macro.DB_INSTANCE_PATH, macro.PID_FILE_NAME)
        self.DN1_PATH = macro.DB_INSTANCE_PATH
        self.dbUserNode = Node(node='dbuser')
        self.rootNode = Node()
        self.com = Common()
        self.Constant = Constant()
        self.sqlfile1 = 'bx1.sql'
        self.sqlfile2 = 'bx2.sql'

    def test_pid_lost(self):
        # 删除pid文件
        del_cmd = f'rm -rf {self.pid_file_path}'
        self.log.info(del_cmd)
        del_msg = self.dbUserNode.sh(del_cmd).result()
        self.log.info(del_msg)
        self.assertEqual(del_msg, '')

        # 查看pid文件（不会自动生成）
        cat_cmd = f'cat {self.pid_file_path}'
        self.log.info(cat_cmd)
        cat_msg = self.dbUserNode.sh(cat_cmd).result()
        self.log.info(cat_msg)
        self.assertIn(self.Constant.NO_FILE_MSG, cat_msg)

        # 目标主机新建路径
        self.to_path = os.path.dirname(self.DN1_PATH) + '/test_script/'
        # 目标主机文件路径
        self.newsql_path1 = os.path.join(self.to_path, self.sqlfile1)
        self.newsql_path2 = os.path.join(self.to_path, self.sqlfile2)
        # 拷贝文件
        self.com.scp_file(self.rootNode, self.sqlfile1, self.to_path)
        self.com.scp_file(self.rootNode, self.sqlfile2, self.to_path)

        # 执行sql并行脚本，查看对数据库的影响
        sql_bx_cmd = f'''
                    source {macro.DB_ENV_PATH}
                    gsql -d {self.dbUserNode.db_name} -p {self.dbUserNode.db_port} -r -f {self.newsql_path1} &
                    gsql -d {self.dbUserNode.db_name} -p {self.dbUserNode.db_port} -r -f {self.newsql_path2} &
                    wait
                    '''
        self.log.info(sql_bx_cmd)
        sql_bx_msg = self.dbUserNode.sh(sql_bx_cmd).result()
        self.log.info(sql_bx_msg)
        self.assertIn('total time: ', sql_bx_msg)

        # 查看pid文件（不会自动生成）
        cat_cmd = f'cat {self.pid_file_path}'
        self.log.info(cat_cmd)
        cat_msg = self.dbUserNode.sh(cat_cmd).result()
        self.log.info(cat_msg)
        self.assertIn(self.Constant.NO_FILE_MSG, cat_msg)

    def tearDown(self):
        # 还原pid文件（启动数据库可生成该文件）
        msg = self.com.kill_pid(self.rootNode, 9)
        self.assertEqual(msg, '')

        is_started = self.sh.startDbCluster()
        self.assertTrue(is_started)

        # 删除scp的脚本
        self.com.scp_file(self.rootNode, self.sqlfile1, self.to_path, True)
        self.com.scp_file(self.rootNode, self.sqlfile2, self.to_path, True)

        self.log.info('---------------Opengauss_Reliability_Dbsys_Case076:用例执行完毕---------------')
